package jubaozhu.controller.admin;

import com.github.pagehelper.PageInfo;
import jubaozhu.domain.Pic;
import jubaozhu.extra.base.AdminBaseController;
import jubaozhu.extra.pojo.PicPo;
import jubaozhu.service.PicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import zhibi.commons.exception.MessageException;
import zhibi.frame.domain.Page;
import zhibi.frame.mybatis.example.Example;
import zhibi.frame.mybatis.example.ExampleType;

import java.util.Date;


@Controller
@RequestMapping("/admin/pic")
public class AdminPicController extends AdminBaseController {

    @Autowired
    private PicService picService;

    /**
     * 列表
     *
     * @param model
     * @param page
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, Page page, PicPo picPo) {
        Example example = Example.getInstance()
                .addParam("p.name", picPo.getName(), ExampleType.Operation.LIKE)
                .addOrder("p.sort")
                .addOrder("p.addtime", ExampleType.OrderType.DESC);
        PageInfo<PicPo> pageInfo = picService.selectPoByExample(example, page);
        setModelAttribute(model, pageInfo);
        return "admin/pic/list";
    }

    /**
     * 到添加页面
     *
     * @param model
     * @return
     */
    @GetMapping("add")
    public String add(Model model) {
        return "admin/pic/add";
    }

    /**
     * 添加
     *
     * @return
     */
    @PostMapping("add")
    public String add(Pic pic, MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            pic.setPath(saveFile(file, "PIC"));
        }
        pic.setStatus(1);
        pic.setAddtime(new Date());
        pic.setUserid(sessionAdmin().getId());
        picService.insertSelective(pic);
        return redirect("list");
    }

    /**
     * 详情
     *
     * @param model
     * @param id
     * @return
     */
    @GetMapping("edit/{id}")
    public String edit(Model model, @PathVariable Integer id) {
        Pic pic = picService.selectByPrimaryKey(id);
        if (null == pic) throw new MessageException("该图片不存在");
        model.addAttribute("pic", pic);
        return "admin/pic/edit";
    }

    /**
     * 更新
     *
     * @param pic
     * @return
     */
    @PostMapping("update")
    public String update(Pic pic, MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            pic.setPath(saveFile(file, "PIC"));
        }
        pic.setUserid(sessionAdmin().getId());
        picService.updateByPrimaryKeySelective(pic);
        return refresh();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id) {
        Pic pic = picService.selectByPrimaryKey(id);
        if (null == pic) throw new MessageException("该图片不存在");
        picService.delete(pic);
        return refresh();
    }

    /**
     * 更新状态
     *
     * @param id
     * @return
     */
    @RequestMapping("updateStatus/{id}")
    public String updateStatus(@PathVariable Integer id) {
        Pic pic = picService.selectByPrimaryKey(id);
        if (null == pic) throw new MessageException("该图片不存在");
        pic.setStatus((pic.getStatus() + 1) % 2);
        pic.setUserid(sessionAdmin().getId());
        picService.updateByPrimaryKeySelective(pic);
        return refresh();
    }
}
